package com.xlhj.yucloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlhj.yucloud.entity.BaseEntity;
import com.xlhj.yucloud.entity.SysUserInfo;
import com.xlhj.yucloud.entity.SysUserRole;
import com.xlhj.yucloud.entity.enums.StatusEnum;
import com.xlhj.yucloud.mapper.SysUserInfoMapper;
import com.xlhj.yucloud.mapper.SysUserRoleMapper;
import com.xlhj.yucloud.service.SysUserInfoService;
import com.xlhj.yucloud.util.DateUtils;
import com.xlhj.yucloud.web.model.UserInfoReqDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalTime;

/**
 * @author: liucaijing
 * @description: SysUserInfoServiceImpl
 * @date: 2021/9/9 21:41
 */
@Service
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUserInfo> implements SysUserInfoService {

    @Autowired
    private SysUserInfoMapper userInfoMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public SysUserInfo findUserByUserName(String userName) {
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getUserName, userName)
                .eq(SysUserInfo::getStatus, 0);
        SysUserInfo userInfo = this.getOne(queryWrapper);
        return userInfo;
    }

    @Override
    public Page<SysUserInfo> selectUserInfoPageList(UserInfoReqDto reqDto) {
        Page<SysUserInfo> userInfoPage = new Page<>(reqDto.getPageNum(), reqDto.getPageSize());
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(reqDto.getUserName())) {
            queryWrapper.like(SysUserInfo::getUserName, reqDto.getUserName());
        }
        if ((StringUtils.hasLength(reqDto.getPhonenumber()))) {
            queryWrapper.eq(SysUserInfo::getPhonenumber, reqDto.getPhonenumber());
        }
        if (!ObjectUtils.isEmpty(reqDto.getStatus())) {
            queryWrapper.eq(SysUserInfo::getStatus, reqDto.getStatus());
        }
        if (StringUtils.hasLength(reqDto.getBeginDate()) && StringUtils.hasLength(reqDto.getEndDate())) {
            queryWrapper.ge(BaseEntity::getCreateTime, DateUtils.getLocalDateTimeFromDateStr(reqDto.getBeginDate(), LocalTime.MIN));
            queryWrapper.le(BaseEntity::getCreateTime, DateUtils.getLocalDateTimeFromDateStr(reqDto.getEndDate(), LocalTime.MAX));
        }
        return this.page(userInfoPage, queryWrapper);
    }

    @Override
    public Long checkUserNameUnique(String userName) {
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getUserName, userName)
                .eq(SysUserInfo::getStatus, StatusEnum.NORMAL);
        Long count = userInfoMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public void deleteUserInfoByIds(Long[] userIds) {
        for (Long userId : userIds) {
            SysUserInfo userInfo = new SysUserInfo();
            userInfo.setId(userId);
            this.removeById(userInfo);
            //删除用户角色表
            LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUserRole::getUserId, userId);
            userRoleMapper.delete(queryWrapper);
        }
    }

    @Override
    public Boolean checkPhoneUnique(Long userId, String phonenumber) {
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getPhonenumber, phonenumber)
                .eq(SysUserInfo::getStatus, StatusEnum.NORMAL);
        SysUserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (null != userInfo && userInfo.getId().longValue() != userId.longValue()) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean checkEmailUnique(Long userId, String email) {
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getEmail, email)
                .eq(SysUserInfo::getStatus, StatusEnum.NORMAL);
        SysUserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (null != userInfo && userInfo.getId().longValue() != userId.longValue()) {
            return true;
        }
        return false;
    }
}
